<?php
namespace app\mini\controller\v1\shop;
use app\common\library\MemberVo;
use app\common\logic\ShopMemberLogic;
use app\common\model\InterfacePermission;
use app\common\model\MemberApply;
use app\common\model\ShopMemberPermission;
use app\mini\controller\Common;
use think\Request;

class ShopMember extends Common
{
    public $ShopMemberLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->ShopMemberLogic = new ShopMemberLogic();
    }


    /**
     * 成员列表
     * @return \think\response\Json
     */
    public function index()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $params['shop_id'] = MemberVo::instance()->getShopId();
            return $this->ShopMemberLogic->index($params);
        });
        return $this->response($res);
    }

    /**
     * 申请列表
     * @return \think\response\Json
     */
    public function apply_index()
    {
        $res = $this->exception(function () {
            return $this->ShopMemberLogic->apply_index(['shop_id'=>MemberVo::instance()->getShopId()]);
        });
        return $this->response($res);
    }

    /**
     * 申请通过
     * @return \think\response\Json
     */
    public function audit()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $params['shop_id'] = MemberVo::instance()->getShopId();
            return $this->ShopMemberLogic->audit($params);
        });
        return $this->response($res);
    }

    /**
     * 权限列表
     * @return \think\response\Json
     */
    public function permissions()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $permissions = InterfacePermission::all();
            $self_permission = ShopMemberPermission::where(['member_id'=>$params['member_id'],'shop_id'=>$this->member_info_arr['default_shop_id']])->column('permission_id');
            foreach ($permissions as &$v){
                $v['selected'] = 0;
                if (in_array($v['id'],$self_permission))
                    $v['selected'] = 1;
            }
            return $permissions;
        });
        return $this->response($res);
    }

    /**
     * 权限更新
     * @return \think\response\Json
     */
    public function permission()
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $info = ShopMemberPermission::get(['permission_id'=>$params['id'],'shop_id'=>$this->member_info_arr['default_shop_id'],'member_id'=>$params['member_id']]);
            if ($info){
                ShopMemberPermission::where(['permission_id'=>$params['id'],'shop_id'=>$this->member_info_arr['default_shop_id'],'member_id'=>$params['member_id'],'id'=>$info['id']])->delete();
            }else{
                ShopMemberPermission::create(['permission_id'=>$params['id'],'shop_id'=>$this->member_info_arr['default_shop_id'],'member_id'=>$params['member_id']]);
            }
            return [];
        });
        return $this->response($res);
    }


    /**
     * 权限更新
     * @return \think\response\Json
     */
    public function permission_update()
    {
        $res = $this->exception(function () {
            return $this->ShopMemberLogic->update_permission(request()->param());
        });
        return $this->response($res);
    }




    /**
     * 待审核数
     * @return \think\response\Json
     */
    public function audits()
    {
        $res = $this->exception(function () {
            $num = MemberApply::where(['shop_id'=>$this->member_info_arr['default_shop_id'],'status'=>MemberApply::CHECK_PENDING])->count('id');
            return ['num'=>$num];
        });
        return $this->response($res);
    }


    /**
     * 删除
     * @return \think\response\Json
     */
    public function del()
    {
        $res = $this->exception(function () {
            return $this->ShopMemberLogic->del(request()->param());
        });
        return $this->response($res);
    }
}